define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'car/index' + location.search,
                    add_url: 'car/add',
                    edit_url: 'car/edit',
                    del_url: 'car/del',
                    multi_url: 'car/multi',
                    table: 'car',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'car_name', title: __('Car_name')},
                        {field: 'car_image', title: __('Car_image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'brand_id', title: __('Brand_id'), searchList: Config.brandList, formatter: Table.api.formatter.label},
                        {field: 'model_id', title: __('Model_id'), searchList: Config.configureList, formatter: Table.api.formatter.label},
                        {field: 'type_id', title: __('Type_id'), searchList: Config.typeList, formatter: Table.api.formatter.label},
                        {field: 'car_site', title: __('Car_site')},
                        {field: 'car_gate', title: __('Car_gate')},
                        {field: 'car_gear', title: __('Car_gear'), searchList: Config.gearlist, formatter: Table.api.formatter.label},
                        {field: 'drive', title: __('Drive'), searchList: Config.driveList, formatter: Table.api.formatter.label},
                        {field: 'displacement', title: __('Displacement'), operate: 'like'},
                        {field: 'status', title: __('Status'), searchList: Config.statuslist, formatter: Table.api.formatter.toggle,yes:1,no:2},
                        {field: 'is_hot', title: __('Is_hot'), searchList: {1:'否',2:'是'}, formatter: Table.api.formatter.toggle,yes:2,no:1},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            var brand_id = $("#c-brand_id").val();
            selectpageCate(brand_id);
            $(document).on("change", "#c-brand_id", function(){
                var brand_id = $("#c-brand_id").val();
                selectpageCate(brand_id);
            });
            function selectpageCate(brand_id){
                $.post("car_cate/index",{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                    custom: {pid: brand_id, type:3},
                    keyField:'id',
                    showField:'cate_name',
                },function(result){
                    //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                    $('#c-type_id').selectPageData(result.list);
                });
            }
            Controller.api.bindevent();
        },
        edit: function () {
            var brand_id = $("#c-brand_id").val();
            selectpageCate(brand_id);
            $(document).on("change", "#c-brand_id", function(){
                var brand_id = $("#c-brand_id").val();
                selectpageCate(brand_id);
            });
            function selectpageCate(brand_id){
                $.post("car_cate/index",{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                    custom: {pid: brand_id, type:3},
                    keyField:'id',
                    showField:'cate_name',
                },function(result){
                    //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                    $('#c-type_id').selectPageData(result.list);
                });
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});